package edu.zut.cs.software.perfect.teacher.web.spring.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.perfect.base.web.spring.controller.GenericController;
import edu.zut.cs.software.perfect.teacher.domain.Teacher;
import edu.zut.cs.software.perfect.teacher.service.TeacherManager;

/**
 * @ClassName TeacherController
 * @Description TODO
 * @Author lixianglong
 * @Date 2020年5月30日 上午12:25:23
 * @Version 1.0
 */

@ResponseBody
@Controller
@RequestMapping("/teacher")
public class TeacherController extends GenericController<Teacher, Long, TeacherManager>{
	TeacherManager teacherManager;
	 @Autowired
	public void setTeacherManager(TeacherManager teacherManager) {
		this.teacherManager = teacherManager;
		this.manager=this.teacherManager;
	}
	
   	@ResponseBody
   	@GetMapping(value = "findall",produces = "applicate/json;charset=utf-8")
   	public String getTeacherAll()
   	{
   		List<Teacher> teacherList=this.manager.findAll();
   		PageInfo pageInfo = new PageInfo(teacherList);
   		   return JSON.toJSONString(pageInfo);
   	}
   @GetMapping("/findbyid/{id}")
   public String findById(@PathVariable("id") Long workid) {
	   Optional<Teacher> result = this.manager.findByWorkId(workid);
	   return JSON.toJSONString(result);
   }
   @GetMapping("/save/{name}")
   public Teacher save(@PathVariable("name") String name)
   {
	   Teacher teacher=new Teacher();
	   teacher.setName(name);
	   Teacher result =teacherManager.save(teacher);
	   return result;
	   
   }
   @GetMapping("/deletebyid/{id}")
   public Boolean deleteById(@PathVariable("id") Long id)
   {
	   Boolean result=this.manager.deleteById(id);
	   return result;
   }
   
   
   
   
   
   @GetMapping("/update/{id}")
   public Teacher update(@PathVariable("id") Long workid)
   {
	   Optional<Teacher> teacher=this.manager.findByWorkId(workid);
	   Teacher teacher_s=teacher.get();
	   teacher_s.setName("li_xiang_long");
	   Teacher result=this.manager.save(teacher_s);
	   return result;
   }
  
	
	
    /*@RequestMapping(value = "/getTeacherById", method = RequestMethod.GET)
	@ResponseBody
	public Teacher getTeacherById(Long workid)
	{
		   return this.teacherManager.findByWorkId(workid);
	}
    @RequestMapping(value = "/addTeacher", method = RequestMethod.POST)
	public String addTeacher(Teacher teacher)
	{
		    this.teacherManager.addTeacher(teacher);
		    return "success";
	}
    @RequestMapping(value = "/deleteTeacher", method = RequestMethod.POST)
   	public String deleteTeacher(long workid)
   	{
    	long a;
    	a=this.teacherManager.findByWorkId(workid).getCourseId();
    	
   		    this.teacherManager.deleteById(a);
   		    return "success";
   	}*/

}
